package com.axjy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Reader;

/**
 * Created by Administrator on 2017/12/25.
 */
public class MyBatisUtil {
    private static SqlSessionFactory sf;
    static {
        if(sf==null){
            Reader reader=null;
            try {
                reader=Resources.getResourceAsReader("mybatis-config.xml");
            } catch (IOException e) {
                e.printStackTrace();
            }
            sf= new SqlSessionFactoryBuilder().build(reader);
        }
    }
    public static SqlSession getSession(){

        return sf.openSession(false);
    }
    public static void closeSession(SqlSession session){
        if(session!=null){
            session.close();
        }
    }
}
